<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <div id="root"></div>


  <script crossorigin src="https://unpkg.com/react@18.3.1/umd/react.development.js"></script>
  <script crossorigin src="https://unpkg.com/react-dom@18.3.1/umd/react-dom.development.js"></script>
  <script crossorigin src="https://unpkg.com/babel-standalone@6.26.0/babel.min.js"></script>

  <script type="text/babel">

    // 模拟组件定义
    const obj = {
      foo: function () {
        console.log(this)
      }
    }
    // React内部传入绑定
    const config = {
      onClick: obj.foo
    }
    // 事件触发时的调用方式
    const click = config.onClick
    click()

    class App extends React.Component {
      constructor() {
        super()
        this.state = {
          counter: 0
        }
        this.handleChangeCounter1 = this.handleChangeCounter1.bind(this)
      }
      handleChangeCounter1() {
        this.setState({ counter: this.state.counter + 1 })
      }
      handleChangeCounter2 = () => {
        this.setState({ counter: 100 })
      }
      handleChangeCounter3 = () => {
        this.setState({ counter: 333 })
      }
      render() {
        const { counter } = this.state
        return (
          <div>
            <h2>counter: { counter }</h2>
            {/* this绑定方式一：使用bind绑定 */}
            <button onClick={this.handleChangeCounter1}>按钮1</button>
            {/* this绑定方式二：ES6-class fields */}
            <button onClick={this.handleChangeCounter2}>按钮2</button>
            {/* this绑定方式二：直接传入一个箭头函数（推荐） */}
            <button onClick={() => this.handleChangeCounter3()}>按钮3</button>
          </div>
        )
      }

    }
    
    const root = ReactDOM.createRoot(document.querySelector('#root'))
    root.render(<App/>)
  </script>
</body>
</html>